<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/1 0001
 * Time: 15:03
 */

namespace App\Base\Models;


use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;

class ResponseModel implements \JsonSerializable, Arrayable, Jsonable
{
    /**
     * 默认返回code
     */
    const DEFAULT_CODE_SUCCESS = 0;
    const DEFAULT_STATUS_SUCCESS = 200;

    /**
     * @var int
     */
    private $code = self::DEFAULT_CODE_SUCCESS;
    private $status=self::DEFAULT_STATUS_SUCCESS;

    private $count = 0;


    private $encypt=0;

    /**
     * @var string
     */
    private $msg;

    /**
     * @var object/array/null/string
     */
    private $data;
    /**
     * 是否为错误信息
     * @var bool
     */
    private $isError = false;



    /**
     * @return int
     */
    public function getCode(): int
    {
        return $this->code;
    }

    /**
     * @param int $code
     */
    public function setCode(int $code): void
    {
        $this->code = $code;
    }

    /**
     * 获取总条数
     * @return int
     */
    public function getCount():int{
        return $this->count;
    }
    /**
     * 总条数
     * @param $count
     */
    public  function setCount($count):void{
        $this->count=$count;
    }

    /**
     * 设置是否加密
     * @param $encypt
     */
    public function setEncypt($encypt){
      $this->encypt=$encypt;
    }

    /**
     * 获取是否加密
     * @return int
     */
    public function getEncypt(){
        return $this->encypt;
    }
    /**
     * @return int
     */
    public function getStatus(): int
    {
        return $this->status;
    }

    public function setStatus($status){
        $this->status = $status;
    }

    /**
     * @return string
     */
    public function getMsg(): string
    {
        return $this->msg;
    }

    /**
     * @param string $msg
     */
    public function setMsg(string $msg): void
    {

        $this->msg = $msg;
    }

    /**
     * @return array|object|string
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param  $data
     */
    public function setData($data): void
    {
        $this->data = $data;
    }

    /**
     * @return bool
     */
    public function isError(): bool
    {
        return $this->isError;
    }

    /**
     * @param bool $isError
     */
    public function setIsError(bool $isError)
    {
        $this->isError = $isError;
    }



    /**
     * Get the instance as an array.
     *
     * @return array
     */
    public function toArray()
    {
        $return=array();
        $return_arr=[
            'code' => $this->code,
            'status' => $this->status,
            'message' => $this->msg,
        ];
        if(!$this->isError){
            $return_arr['data'] = $this->data;
        }
        if($this->count>=0){
            $return_arr['count'] = $this->count;
        }

        if($this->encypt){
           return $this->encyptDataReturn($return_arr);
        }
        return $return_arr;
    }

    /**
     * 数据进行加密
     * @param $return_arr
     */
    public function encyptDataReturn($return_arr){
      return  ['data'=>base64_encode(json_encode($return_arr))];
    }

    /**
     * Convert the object to its JSON representation.
     *
     * @param  int $options
     * @return string
     */
    public function toJson($options = 0)
    {
        $data = json_encode($this->toArray());
        if (json_last_error() && json_last_error() == JSON_ERROR_UTF8) {
            $data = json_encode($this->utf8Encode($this->toArray()));
        }
//        $error = json_last_error_msg();
        return $data;
    }

    /**
     * Encode array to utf8 recursively
     * @param $dat
     * @return array|string
     */
    private function utf8Encode($dat)
    {
        if (is_string($dat))
            return utf8_encode($dat);
        if (!is_array($dat))
            return $dat;
        $ret = array();
        foreach ($dat as $i => $d) {
            $ret[$i] = $this->utf8Encode($d);
        }
        return $ret;
    }

    /**
     * Specify data which should be serialized to JSON
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
     * @return mixed data which can be serialized by <b>json_encode</b>,
     * which is a value of any type other than a resource.
     * @since 5.4.0
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }

    /**
     * @return string
     */
    public function __toString()
    {
        return $this->toJson();
    }


}